package cn.lijiancn01.demo.framework.config.advice;

import cn.dev33.satoken.exception.NotLoginException;
import cn.lijiancn01.demo.framework.exception.BizException;
import cn.lijiancn01.demo.framework.model.R;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class MyControllerAdvice {
    final Marker marker = MarkerFactory.getMarker("MyControllerAdvice");

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        if (e instanceof BizException bizException) {
            return R.error(bizException.getMessage());
        } else if (e instanceof NotLoginException notLoginException) {
            return R.error(401, notLoginException.getMessage());
        } else {
            log.error(marker, "handleException", e);
            return R.error(e.getMessage());
        }

    }
}
